/**
 * Generic Fact search class that builds the url and ties together HttpRetriever and XmlParser objects
 * 
 **/
package com.syde322;

import java.net.URLEncoder;
import java.util.ArrayList;

public abstract class FactSearch<E> {

	protected static final String BASE_URL = "http://en.wikipedia.org/w/api.php?format=xml&action=opensearch&search=";    
    protected static final String LOCATION = "";
    
    protected static HttpRetriever httpRetriever = new HttpRetriever();
    protected static XmlParser xmlParser = new XmlParser();
        
	public abstract ArrayList<E> find(String query);
	public abstract ArrayList<E> find(String query, int maxResults);
	
    protected static String constructSearchUrl(String query) {
        StringBuffer sb = new StringBuffer();
        sb.append(BASE_URL);
        sb.append(LOCATION);
        sb.append(URLEncoder.encode(query));
        return sb.toString();
    }
    
    public ArrayList<WikiFacts> retrieveFirstResults(ArrayList<WikiFacts> list, int maxResults) {
        ArrayList<WikiFacts> newList = new ArrayList<WikiFacts>();
        int count = Math.min(list.size(), maxResults);
        for (int i=0; i<count; i++) {
            newList.add(list.get(i));
        }
        return newList;
    }
    
}
